package pablo.developer;

import java.util.ArrayList;
import java.util.List;

import pablo.developer.R;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;




public class Mapa extends MapActivity {
	ArrayList<Resultados> Elementos = new ArrayList<Resultados>();
	Localizacion locOrigen,locDestino;
	private MapController mapController;
	private MyLocationOverlay myLocationOverlay;
	private MapView mapView;
	protected List<Overlay> mapOverlays;
	private Button btnSalir = null;
	
	@SuppressWarnings("unchecked")
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapa);
        
        Bundle bundle  = getIntent().getExtras();
		if (bundle!= null){ 
			Elementos  = (ArrayList<Resultados>) bundle.getSerializable("Resultados");
			locOrigen  = (Localizacion) bundle.getSerializable("LocationOrigen");
			locDestino = (Localizacion) bundle.getSerializable("LocationDestino");
		 }
		inicializeMap();
	}
	
	private void inicializeMap(){		
		btnSalir = (Button)findViewById(R.id.BtnSalir);
		int latitud = (int) (locOrigen.lat * 1E6);
		int longitud = (int) (locOrigen.lng * 1E6);
     	mapView = (MapView) findViewById(R.id.map);
        mapView.setBuiltInZoomControls(true);        
        mapController = mapView.getController();
        mapController.setZoom(9);
        mapController.animateTo(new GeoPoint(latitud,longitud));
        mapOverlays = mapView.getOverlays();
        Drawable drawable = getResources().getDrawable(R.drawable.balloon_overlay_bg);
        OverlayItem overlayItem = new OverlayItem(new GeoPoint(latitud,longitud),"Ruta del Usuario: "+ Elementos.get(0).getNick(), "Ruta Confirmada");     
        MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable, mapView.getContext(), mapView);
        itemizedoverlay.addOverlay(overlayItem);
        List<GeoPoint> list = new ArrayList<GeoPoint>(); 
	    list.add(new GeoPoint((int) (locOrigen.lat*1E6),(int) (locOrigen.lng*1E6)));
	    list.add(new GeoPoint((int) (locDestino.lat*1E6), (int) (locDestino.lng*1E6)));        
	    for(int i = 0; i < list.size(); i++){
	     if(i>0){
	      mapOverlays.add(new LinePathOverlay(list.get(i-1), list.get(i)));
	     }
	    }
        mapOverlays.add(itemizedoverlay);
        centerMyPosition();
        
	}
		private void centerMyPosition(){
		    myLocationOverlay = new MyLocationOverlay(this, mapView);
		    mapView.getOverlays().add(myLocationOverlay);
		    myLocationOverlay.enableCompass();
		    myLocationOverlay.enableMyLocation();
		    myLocationOverlay.runOnFirstFix(new Runnable() {
		        public void run() {
		            mapController.animateTo(myLocationOverlay.getMyLocation());
		        }
		    });
		}

	  
	public void salir(View V){
		 Intent mainIntent = new Intent(this,MenuActivity.class);
		 Mapa.this.startActivity(mainIntent);
		 Mapa.this.finish();
		
		
	}
		
	@Override
	protected boolean isRouteDisplayed() {
		return true;
	}
}
